home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 658 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  70 lines

  1. Path: cs.mu.OZ.AU!bounce-back
  2. From: austern@isolde.mti.sgi.com (Matt Austern)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: typename?
  5. Date: 08 Mar 96 03:23:54 GMT
  6. Organization: SGI
  7. Approved: fjh@cs.mu.oz.au
  8. Message-ID: <AUSTERN.96Mar7183544@isolde.mti.sgi.com>
  9. References: <m0tupLs-000CajC@sqarc.sq.com>
  10. Reply-To: austern@cardboard.mti.sgi.com
  11. NNTP-Posting-Host: munta.cs.mu.oz.au
  12. X-Original-Date: 08 Mar 1996 02:35:44 GMT
  13. In-Reply-To: willer@carolian.com's message of 08 Mar 96 02:10:08 GMT
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBFAgUBMT+oWuEDnX0m9pzZAQFlWgF/RqKnJx+GiRm/anS+TbETJir01s8FA2/9
  16.     uFjeE77GIgp3iRGgs3Nj1X1b773bmeUg
  17.     =WrRF
  18. Originator: fjh@munta.cs.mu.OZ.AU
  19.  
  20. In article <m0tupLs-000CajC@sqarc.sq.com> willer@carolian.com (Steve Willer)
  21. writes:
  22.  
  23. > I saw in the brochure for Borland C++ 5.0 that it will include support for
  24. > numerous new C++ features, including the "typename" keyword. Could anyone
  25. > enlighten me as to what that is? I couldn't find it in the April WP.
  26. > This isn't equivalent to the "typeof" keyword that I want so much, is it?
  27.  
  28. No.  The typename keyword is a consequence of the new rules for name
  29. resolution in templates.  Brief summary: there's a new disambiguation
  30. rule saying that a name used in a template is assumed not to be a type
  31. unless it has been explicitly declared to be a type.  
  32.  
  33. Disambiguation rules like this are good, since they make it possible
  34. to check template syntax even before the template has been
  35. instantiated.  This one, though, presents a problem.  Suppose you want
  36. to write code like this.
  37.     template<class Container>
  38.     void f(Container X)
  39.     {
  40.        Container::value_type tmp = *(X.begin());
  41.        // do something with tmp.
  42.     }
  43. This is a syntax error, since Container::value_type is assumed not to
  44. be the name of a type.  (And I hope everyone agrees that this isn't a
  45. contrived example.  The STL source is full of examples like this.)
  46.  
  47. The typename keyword was introduced to solve this problem; it says
  48. that the name following it refers to a type.  The correct way to write
  49. this code snippet, then, is as follows.
  50.     template<class Container>
  51.     void f(Container X)
  52.     {
  53.        typename Container::value_type tmp = *(X.begin());
  54.        // do something with tmp.
  55.     }
  56.  
  57. This is described in section 14.2 [temp.res] of the WP.
  58. -- 
  59. Matt Austern
  60. SGI: MTI Compilers Group
  61. austern@isolde.mti.sgi.com
  62. ---
  63. [ To submit articles: try just posting with your news-reader.
  64.                       If that fails, use mailto:std-c++@ncar.ucar.edu
  65.   FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  66.   Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  67.   Comments? mailto:std-c++-request@ncar.ucar.edu.
  68. ]
  69.